const cheerio = require("cheerio");
const axios = require("axios");
const ffmpeg = require('fluent-ffmpeg')
const path = require('path');

function fetchBaiduVideoUrl(data) {
  let $ = cheerio.load(data);
  let urlNode = $("#_page_data")[0]["children"][0]["data"];
  let title = $('title').text().split(',')[0]
  let reg = new RegExp('"playurl":(.*)');
  let paralist = reg.exec(urlNode)[1].split(",");
  let videoUrl = paralist[0].replace(/[",\\]/g, "");
  return {videoUrl,title};
}

function downloadBaiduVideo(url) {
  return new Promise(function (reslove, reject) {
    axios({
      method: "get",
      url: url,
    })
      .then(function (response) {
        return fetchBaiduVideoUrl(response.data);
      })
      .then(function (result) {
        reslove(result)
      })
  });
}

module.exports = {
  downloadBaiduVideo,
};
